package com.ztjava.article.controller;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.JsonKit;
import com.ztjava.article.model.ArticleCatalog;
import com.ztjava.article.model.ArticleComment;
import com.ztjava.article.model.ArticleContent;
import com.ztjava.article.service.ArticleService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

@ControllerClass(client=PortalClient.class,path="/article")
@UserController(portal=Constant.P_PORTAL)
public class ArticlePortalController extends BaseController{
	/*************
	 *ArticlePortalController.java
	 *@AutoJavaCode
	 *2017-04-23 11:42:04
	**************/
	private static ArticleService articleService = Duang.duang(ArticleService.class);
	/*************
	 *修改Catalog
	**************/
	@UserMethod("ArticleCatalog")
	public void addEditCatalog(){
		Integer id = getParaToInt("id",0);
		ArticleCatalog articleCatalog = new ArticleCatalog();
		if(id>0){
			articleCatalog = ArticleCatalog.dao.findById(id);
			if(articleCatalog.getInt("parent_id")>0){
				ArticleCatalog parentCatalog = ArticleCatalog.dao.findById(articleCatalog.getInt("parent_id"));
				setAttr("parentCatalog", parentCatalog);
			}
		}else{
			Integer parent_id = getParaToInt("parent_id",0);
			articleCatalog.set("parent_id", parent_id);
			if(parent_id>0){
				ArticleCatalog parentCatalog = ArticleCatalog.dao.findById(parent_id);
				articleCatalog.set("catalog_tpl", parentCatalog.getStr("catalog_tpl"));
				articleCatalog.set("list_tpl", parentCatalog.getStr("list_tpl"));
				articleCatalog.set("show_tpl", parentCatalog.getStr("show_tpl"));
				setAttr("parentCatalog", parentCatalog);
			}
		}
		setAttr("articleCatalog",articleCatalog);
		render("addEditCatalog.html");
	}

	/*************
	 *保存Catalog
	**************/
	@UserMethod("ArticleCatalog")
	public void saveCatalog(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		ArticleCatalog articleCatalog = getModel(ArticleCatalog.class);
		saveModel(articleCatalog,authSession.getId());
			if(articleCatalog.getInt("parent_id")>0){
				ArticleCatalog parent = ArticleCatalog.dao.findById(articleCatalog.getInt("parent_id"));
				articleCatalog.set("num", parent.getStr("num")+articleCatalog.getInt("id")+",");
			}else {
				articleCatalog.set("num", articleCatalog.getInt("id")+",");
			}
			articleCatalog.update();
			setAttr(Constant.RESULT, Constant.SUCCESS);
			setAttr(Constant.MESSAGE, "保存成功！");
			setAttr("object", articleCatalog);
			renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"object"});
	}

	/*************
	 *Catalog列表
	**************/
	@UserMethod("ArticleCatalog")
	public void catalogList(){
		
		List<ArticleCatalog> list = ArticleCatalog.dao.find("select id,parent_id as pId,name from "+ArticleCatalog.tablename+" where 1=1 "
				+" order by sort asc,id asc");
		setAttr("listJSON", JsonKit.toJson(list));
		render("catalogList.html");
	}

	/*************
	 *删除Catalog
	**************/
	@UserMethod("ArticleCatalog")
	public void deleteCatalog(){
		Integer id = getParaToInt("id",0);
		ArticleCatalog child = ArticleCatalog.dao.findFirst("select id from "+ArticleCatalog.tablename+" where parent_id=?",id);
		if(child!=null){
			renderError("存在子分类不允许删除！");
			return;
		}
		ArticleCatalog.dao.deleteById(id);
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "删除成功！");
		setAttr("id", id);
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"id"});
	}


	/*************
	 *修改Content
	**************/
	@UserMethod("ArticleContent")
	public void addEditContent(){
		Integer id = getParaToInt("id",0);
		ArticleContent articleContent = new ArticleContent();
		if(id>0){
			articleContent = ArticleContent.dao.findById(id);
		}else{
			
				articleContent.set("catalog_id",getParaToInt("catalog_id",0));
		}
		setAttr("articleContent",articleContent);
		render("addEditContent.html");
	}
	
	/*************
	 *保存Content
	**************/
	@UserMethod("ArticleContent")
	public void saveContent(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		ArticleContent articleContent = getModel(ArticleContent.class);
		saveModel(articleContent,authSession.getId());
			renderSuccess();
	}
	
	/*************
	 *Content列表
	**************/
	@UserMethod("ArticleContent")
	public void contentList(){
		Integer catalog_id = getParaToInt("catalog_id");
		setAttr("catalog_id",catalog_id);
		if(!getPara("is_ajax","F").equals("T")){
			List<ArticleCatalog> list = ArticleCatalog.dao.find("select id,parent_id as pId,name,num from "+ArticleCatalog.tablename+" order by sort asc,id asc");
			setAttr("listJSON", JsonKit.toJson(list));
			render("contentList.html");
			return;
		}
		renderPageJson(articleService.contentPage(client, getParaMap()));
	}
	
	/*************
	 *批量删除Content
	**************/
	@UserMethod("ArticleContent")
	public void batchDeleteContent(){
		String[] btSelectItem = getParaValues("btSelectItem");
		articleService.batchDelete(ArticleContent.tablename, btSelectItem);
		renderSuccess();
	}
	
	/*************
	 *Comment列表
	**************/
	@UserMethod("ArticleComment")
	public void commentList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("commentList.html");
			return;
		}
	
		
		renderPageJson(articleService.getCommentPage(client, getParaMap()));
	}
	
	/*************
	 *批量删除Comment
	**************/
	@UserMethod("ArticleComment")
	public void batchDeleteComment(){
		String[] btSelectItem = getParaValues("btSelectItem");
		articleService.batchDelete(ArticleComment.tablename, btSelectItem);
		renderSuccess();
	}
}
